<template>
  <HomePanel title="新鲜好物" subTitle="赶紧来看">
    <template #right>
      <XtxMore path="/"/>
    </template>
    <div ref="target" style="position: relative;height: 426px;">
      <!-- 商品主体内容区域 -->
      <Transition name="fade">
        <ul ref="target" class="goods-list" v-if="goods.length">
          <li v-for="item in goods" :key="item.id">
            <RouterLink :to="`/product/${item.id}`">
              <img :src="item.picture" alt="">
              <p class="name ellipsis">{{item.name}}</p>
              <p class="price">&yen;{{item.price}}</p>
            </RouterLink>
          </li>
        </ul>
        <!-- 骨架效果 -->
        <HomeSkeleton bg="#f0f9f4" v-else/>
      </Transition>
    </div>
  </HomePanel>
</template>

<script>
import HomePanel from '@/views/home/components/home-panel.vue'
import { findNew } from '@/api/home.js'
import HomeSkeleton from '@/views/home/components/home-skeleton.vue'
import { useLazyData } from '@/hooks/index.js'
export default {
  name: 'HomeNew',
  components: {
    HomePanel,
    HomeSkeleton
  },
  setup () {
    // // 商品初始数据
    // const goods = ref([])
    // // 请求商品数据并且赋值渲染
    // findNew().then((data) => {
    //   goods.value = data.result
    // })

    // 调用懒加载函数，获得数据、以及空值方便绑定dom元素
    const { target, result } = useLazyData(findNew)
    return { goods: result, target }
  }
}
</script>

<style lang="less" scoped>
  .goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding: 12px 30px 0 30px;
      text-align: center;
    }
    .price {
      color: @priceColor;
    }
  }
}
</style>
